// pages/shoucang/shoucang.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    item: [],
    sum: 0,
    IsNull: 0,
    kkk:['店长推荐','爆款热卖','宝藏商品','值得一买','来拼单吧','特惠单品'],
    tttt:['值得一试','火热预约','清凉夏日']
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.phone != '') {
      //上门服务
      var car = []
      var sum = app.globalData.product_sum
      var carts = app.globalData.product
      var service_items_sum = app.globalData.service_items_sum
      var service_items = app.globalData.service_items
      var that = this
      let tableName = 'love'

      let Query = new wx.BaaS.Query()
      Query.compare('phone', '=', app.globalData.phone)
      let Product = new wx.BaaS.TableObject(tableName)
      Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
        // success
        console.log(res)
        console.log(res.data.objects[0].product_code.length)
        that.setData({
          items: res.data.objects, //上门服务全部数据
          // sum:res.data.objects.product_code.length
        })
        for (var i = 0; i < that.data.items[0].product_code.length + that.data.items[0].items.length - 1; i++) { //全部购物车个数
          // console.log(that.data.items[0].product_code[i])
          for (var j = 0; j < sum; j++) {
            // console.log(carts[j].product_code)
            if (carts[j].product_code == that.data.items[0].product_code[i]) {
              var a = Math.floor((Math.random() * 5))
              var item = {
                name: carts[j].product_name,
                img: carts[j].product_swiper_pic1,
                cost: carts[j].cost,
                pre_cost: carts[j].pre_cost,
                // product_ID: that.data.cart[j].id //数据行id
                product_code: that.data.items[0].product_code[i],
                product_introduce:that.data.kkk[a]
              }
            }
          }
          // console.log(that.data.items[0].items[i])
          for (var k = 0; k < service_items_sum; k++) {
            if (service_items[k].item_name == that.data.items[0].items[i]) {
              var a = Math.floor((Math.random() * 3))
              var item = {
                name: service_items[k].item_name,
                img: service_items[k].item_pic,
                cost: service_items[k].actual_price,
                pre_cost: service_items[k].all_price,
                item_name: that.data.items[0].items[i],
                product_introduce:that.data.tttt[a]
              }
            }
          }
          car.push(item)
          console.log(item)
        }
        that.setData({
          item: car
        })
        console.log(car)
        if (car == undefined) {
          that.setData({
            IsNull: 1
          })
        }
      }, err => {
        // err
      })
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      setTimeout(function (e) {
        wx.switchTab({
          url: '/pages/myself/myself',
        })
      }, 1000)
    }

  },

  goDetail: function (e) {
    var that = this
    var postid = e.currentTarget.dataset.postid
    console.log(postid)
    if (that.data.item[postid].product_code != '') {
      app.navigateToDeTail(that.data.item[postid].product_code)
    } else {
      app.navigateToService(that.data.item[postid].item_name)
    }

  }
})